package com.example.Windturbine.Controller;

import com.example.Windturbine.Controller.ViewObject.ProductUseVO;
import com.example.Windturbine.Dao.ProductDOMapper;
import com.example.Windturbine.DataObject.ProductDO;
import com.example.Windturbine.DataObject.ProductInfoDO;
import com.example.Windturbine.DataObject.ProductUseDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.ProductCalService;
import com.example.Windturbine.Service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/21
 */

@Slf4j
@Controller("cal")
@RequestMapping("/cal")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class ProductCalController {

    @Autowired
    ProductCalService productCalService;

    @Resource
    ProductDOMapper productDOMapper;


    @RequestMapping(value = "/submit", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded"})
    @ResponseBody
    public CommonResult submitProduct(@RequestParam(name = "cal_name") String cal_name,
                                      @RequestParam(name = "cal_num") BigDecimal cal_num,
                                      @RequestParam(name = "cal_power") BigDecimal cal_power,
                                      @RequestParam(name = "cal_time") BigDecimal cal_time,
                                      @RequestParam(name = "cal_timeunit") String cal_timeunit,
                                      @RequestParam(name = "cal_water") BigDecimal cal_water,
                                      @RequestParam(name = "cal_gasoil") BigDecimal cal_gasoil,
                                      @RequestParam(name = "cal_diesel") BigDecimal cal_diesel,
                                      @RequestParam(name = "cal_gas") BigDecimal cal_gas,
                                      @RequestParam(name = "cal_comments") String cal_comments,
                                      @RequestParam(name = "cal_method") Integer cal_method,
                                      @RequestParam(name = "cal_productid") Integer cal_productid,
                                      @RequestParam(name = "cal_userid") Integer cal_userid) throws BusinessException, IOException, NoSuchAlgorithmException {

        //提交计算条件
        ProductUseDO productUseDO=new ProductUseDO();
        productUseDO.setName(cal_name);
        productUseDO.setTime(cal_time);
        productUseDO.setPower(cal_power);
        productUseDO.setUsenum(cal_num);
        productUseDO.setTimeUnit(cal_timeunit);
        productUseDO.setWater(cal_water);
        productUseDO.setGasoline(cal_gasoil);
        productUseDO.setDieseloil(cal_diesel);
        productUseDO.setGas(cal_gas);
        productUseDO.setComments(cal_comments);
        productUseDO.setMethod(cal_method);
        productUseDO.setProductId(cal_productid);
        productUseDO.setUserId(cal_userid);
        boolean submit = productCalService.submit(productUseDO);
        if(submit){
            return CommonResult.create(null);
        }else{
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"计算失败！");
        }

    }


    @RequestMapping(value = "/getAll")
    @ResponseBody
    public CommonResult getAll(@RequestParam(name = "id") Integer user_id) throws BusinessException {
        List<ProductUseDO> productUseDOS= productCalService.getAll(user_id);

        return CommonResult.create(convertFromList(productUseDOS));
    }


    private List<ProductUseVO> convertFromList(List<ProductUseDO> productUseDOS){
        List<ProductUseVO> list=new ArrayList<>();
        for(ProductUseDO p:productUseDOS){
            list.add(convertFromDO(p));
        }
        return list;

    }


    private ProductUseVO convertFromDO(ProductUseDO productUseDO){
        Integer id=productUseDO.getProductId();
        ProductDO productDO=productDOMapper.selectByPrimaryKey(id);
        ProductUseVO productUseVO=new ProductUseVO();
        BeanUtils.copyProperties(productUseDO,productUseVO);
        productUseVO.setProduct_name(productDO.getName());
        return productUseVO;
    }


}
